<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property string $id
 * @property string $role_id
 * @property string $username
 * @property string $password
 * @property string $created
 * @property string $modified
 * @property integer $isActive
 * @property integer $isLogin
 * @property string $lastVisit
 *
 * The followings are the available model relations:
 * @property Role $role
 */
class User extends CActiveRecord
{
    public $password_repeat;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('role_id, username, password, password_repeat', 'required', 'on'=>'insert'),
			array('role_id', 'required', 'on'=>'update'),
                        array('password_repeat', 'compare', 'compareAttribute'=>'password'),
			array('role_id', 'length', 'max'=>11),
			array('username, password', 'length', 'max'=>100),
			array('password_repeat, password', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, role_id, username, password, created, modified, isActive, isLogin, lastVisit', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'role' => array(self::BELONGS_TO, 'Role', 'role_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'role_id' => 'Role',
			'username' => 'Username',
			'password' => 'Password',
			'password_repeat' => Yii::t('view','Ulangi Password'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('role_id',$this->role_id,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        
        public function beforeSave() 
        {
            if($this->isNewRecord)
            {           
                 $this->created=new CDbExpression('NOW()');
                 $this->modified = new CDbExpression('NOW()'); 
                 $this->password = $this->hashPassword($this->password);
            }else{
                 $this->modified = new CDbExpression('NOW()'); 
                 if(!empty($this->password)){
                            $this->password = $this->hashPassword($this->password);
                    }
            }

            return parent::beforeSave();
        }
        
        /**
	 * Checks if the given password is correct.
	 * @param string the password to be validated
	 * @return boolean whether the password is valid
	 */
	public function validatePassword($password)
	{
		return CPasswordHelper::verifyPassword($password,$this->password);
	}

	/**
	 * Generates the password hash.
	 * @param string password
	 * @return string hash
	 */
	public static function hashPassword($password)
	{
		return CPasswordHelper::hashPassword($password);
	}
}
